/**
 * 采购管理模块
 * 
 * @Date: 2016-9-8
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(['jquery', 'init','fullcalendar','fullcalendar_zh-cn','moment','bsUtil','bsButtonEvent','procurementUtils',
			'bsDropDownMenu','bsMsgServ','bsProcurementAssetLittleEdit','contextMenu',"bsButton"], 
	function($,init,fullcalendar,calendarLang,moment,bsUtil,bsButtonEvent,procurementUtils,
				bsDropDownMenu,bsMsgServ,bsProcurementAssetLittleEdit,contextMenu,bsButton) {

	'use strict';

	var listUrl = '/procurementDirect/query/getAssetLittleListDomain.action',
		
		
		
		delUrl='/procurementDirect/del.action',
		
		//模块代码定义
		moduCode = 'procurementAssetLittle',
		
		//编辑页面url
		procdetailEditUrl='/procurementDirect/query/getPage.action?code=bsProcdetailDlg'
	//js参数
	var jsParam
	
	//文档分类树dom对象,文档明细列表对象
	var $grid
	
	//弹窗dom对象
	var $edit_window
	
	//选中类型对象
	var selectedType
	
	var rootid = 'bs-procurement-asset-little';
	var private_method = {
		
		
		/**
		 * 初始化明细列表
		 */
		initList:function(){
			var _detailParam = $.extend({},procurementUtils.getGridParam(),{				
				url : '/'+init.getSite() + listUrl,				
				onDblClickRow: btnEvent.dblClick})
				
			var _d = $('#'+rootid + ' .dg-list')
			if(_d.length!=1){
			
				_d = $('#'+rootid + ' .list-detail')
				_d.append('<table class="dg-list"></table>');
			}
			$grid = $('#'+rootid + ' .dg-list').datagrid(_detailParam);			
			
		},
		
		initListBtn:function(){
			if(window.console){
				console.log(' grid list button init ...')
			}
			var _btn = bsButton.getBtn({bizcode:moduCode,btnType:'list',bizType:2});
			
				
			if(_btn){
				var _c = $('#'+rootid + ' .btn-container')
				if(_c.length>0){
					_c.empty();
					_c.append(_btn);
				}
				
				private_method.bind_btn_click();
				
				
			}
			
		}		
		,
		/**
		 * 从其他页面返回列表页面
		 */
		backPage:function(){
			$('#'+rootid + ' .list-detail').empty()
			private_method.initListBtn();
			private_method.initList();
			private_method.bind_search_click();
			
		}
		,
		
		bind_btn_click:function(e){
			var _btn = $('#'+rootid + ' .btn')
			if(_btn.length>0){
				_btn.unbind('click');
				_btn.bind('click',private_method.btn_listenner);
			}
			

		}
		
		,
		bind_search_click:function(){
			var _btn = $('#'+rootid + ' button[btn-type=search]')
			if(_btn.length>0){
				_btn.unbind('click');
				_btn.bind('click',btnEvent.search);
			}
		}
		,
		btn_listenner:function(e){
			init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
		}
		
		
		,
				
		/**
		 * 执行审核
		 * @param _options.auditType	审核类型
		 * @param _options.evt			事件对象
		 */
		exeAudit:function(_options){
			
			var e = _options.evt;
			
			var _grid = $('#'+rootid + ' .dg-list')
			var records = _grid.datagrid('getSelections');
			if(records && records.length>0){
				if(!bsUtil.checkStatus({records:records,status:0})){								
					bsMsgServ.warnSlide('已审核记录不能进行操作！');
					$(e.currentTarget).removeClass('disabled');
					return
				}
				
				var _data = {};	
				
				switch(_options.auditType){
					case 'permit':
					_data.status=1
					break;
					
					case 'denied':
					_data.status=-1
					break;
					
					case 'submit':
					_data.status=0
					break;
					default:
					break;
				}
				
				
				if(records && records.length>0){
					_data = $.extend({},_data,private_method.getCheckData(records));
					
					//处理单条记录和批量记录id值
					if(_data.id){
						_data.ids = _data.id;
					}
					$.ajax({
						url:'/'+init.getSite()+'/docdetailAudit/auditList.action',
						data:_data,
						success:function(data){
							
							bsMsgServ.msgSlide('操作成功！');								
							
							$('#'+rootid + ' .dg-list').datagrid('reload')
							$(e.currentTarget).removeClass('disabled');
						}
						,
						failure:function(){
							bsMsgServ.errorSlide('操作失败！');							
							$(e.currentTarget).removeClass('disabled');
						}
					})
				}
			}else{
				bsMsgServ.warnSlide('请选择待执行数据');
				$(e.currentTarget).removeClass('disabled');
			}
		}
		,
		
		getCheckData:function(records){
			var _data,tid
			if(records.length==1){
				_data = {id:records[0].id};	
			}else{
				$.each(records,function(i,el){
					tid=!tid?el.id:tid+','+el.id
				})
				_data = {ids:tid}
			}
			
			return _data;
		}
		,
		//获取查询参数
		getSercehValue:function(){
			var _el = $('#'+rootid + ' .search-form #name');
			var _name;
			if(_el.length==1){
				_name = _el.val();
				return _name;
			}
		},
		
		hideSearch:function(){
			$('#'+rootid + ' .search-form').hide();
			
		}
		
	}
	
	var btnEvent =  {
		listAddReplace:function(e){			
			
			$('#'+rootid + ' .dg-list').datagrid('clearSelections');
			btnEvent.editExecute({evt:e,flag:false});			
		}
		,
		listDel:function(e){
			
			var _data,tid
			
			var _grid = $('#'+rootid + ' .dg-list')
			var records = _grid.datagrid('getSelections');
			if(records.length>0){
				var selectedRecord = records[0];
				//判断审核标志
				
				if(!bsUtil.checkStatus({records:records,status:0})){								
					bsMsgServ.warnSlide('已审核记录不能进行删除操作！');
					$(e.currentTarget).removeClass('disabled');
					return
				}
				
				
				
				if(records.length==1){
					_data = {id:selectedRecord.id};	
				}else{
					$.each(records,function(i,el){
						tid=!tid?el.id:tid+','+el.id
					})
					_data = {ids:tid}
				}
				
			}else{
				bsMsgServ.warnSlide('请选择要删除的记录');
				$(e.currentTarget).removeClass('disabled');
				return
			}
								
			$.messager.confirm('警告', '确定执行删除操作?', function(r){
						
				if( r){					
					$.ajax({
						url:'/'+init.getSite()+ delUrl,
					 	contentType:'form',
					 	data:_data,
					 	success:function(){						 		
							_grid.datagrid('clearSelections');
							_grid.datagrid('reload');
							bsMsgServ.msgSlide('删除记录操作成功！');
							$(e.currentTarget).removeClass('disabled');
					 	}
					 	,
					 	failure:function(){
					 		
					 		bsMsgServ.errorSlide(
					 			codeDef.getConfig({
					 				type:'operate',code:'error'
					 			})
					 		);
					 		$(e.currentTarget).removeClass('disabled');
					 	}
					})
				}
			})
		}
		,
		listEdit:function(e){
			
			var data =$('#'+rootid + ' .dg-list').datagrid('getSelections')
			
			if(data.length==1){
				btnEvent.editExecute({row:data[0],evt:e})
			}else{
				bsMsgServ.errorSelect(data);
				$(e.currentTarget).removeClass('disabled');
			}
		}
		,
		
		dblClick:function(index,row){
			
			var _grid = $('#'+rootid + ' .dg-list')
			_grid.datagrid('clearSelections')
			_grid.datagrid('selectRow',index);
			
			btnEvent.editExecute({row:row});
		}
		,
		/**
		 * @param _options.evt	事件对象
		 * @param _options.flag	新增或编辑标志
		 */
		editExecute : function(_options) {	
			
						
			//查找模块配置
			
			
			var _param = {				
							
				callback:private_method.backPage,
				row:_options.row
			}		
			
			
			bsProcurementAssetLittleEdit.replaceInit(_param);
			
			if(_options.evt){
				$(_options.evt.currentTarget).removeClass('disabled');
			}
			
			private_method.hideSearch();
		}
		
		,
				
		/**
		 * 提交审核
		 */
		listSubmit:function(e){
			
			private_method.exeAudit({auditType:'submit',evt:e})
			
		},
		/**
		 * 审核通过
		 */
		listPermit:function(e){
			
			private_method.exeAudit({auditType:'permit',evt:e})
		},
		/**
		 * 审核未通过
		 */
		listDenied:function(e){
			
			private_method.exeAudit({auditType:'denied',evt:e})
		},
		
		search:function(e){
			//获取文档明细列表
			var _name = private_method.getSercehValue();
			
			var _param = {};
			if(_name){
				_param.name_like = _name;
			}			
			
			$grid.datagrid('load',{condition:_param})	
			
		}
		,
		operate:function(e){
			
		}
		
	}
	
	var public_method = {
		
		//初始化函数
		initPage:function(){
			private_method.initList();
			private_method.initListBtn();			
			private_method.bind_search_click();
		}
		,
		//脚本打开页面
		openPage:function(_options){
			var _url = '/'+init.getSite()+'/common/query/getPage.action?code=userSelect';
			
			var _container = $('#user-select-pop')
			
			if(_container.length<=0){
				$('body').append('<div id="user-select-pop"></div>');
			}
			
			_container = $('#user-select-pop')
			_container.load(_url);
		}
	}
	
	
	return public_method;
});